﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class ManualTool_Weixiu_Return : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        string str, 項次, 手工具編號, 手工具名稱, 領用日期, 受理人, 歸還日期, 領用數量;
        public ManualTool_Weixiu_Return(string str, string 項次, string 手工具編號, string 手工具名稱, string 領用日期, string 受理人, string 歸還日期, string 領用數量)
        {
            InitializeComponent();
            this.受理人 = 受理人;
            this.手工具名稱 = 手工具名稱;
            this.手工具編號 = 手工具編號;
            this.歸還日期 = 歸還日期;
            this.項次 = 項次;
            this.領用日期 = 領用日期;
            this.領用數量 = 領用數量;
            this.str = str;
        }

        private void bt_歸還_Click(object sender, EventArgs e)
        {
            if (str.Equals("維修"))
            {
                update("sp_手工其維修_return");
            }
            if (str.Equals("送檢"))
            {
                update("sp_手工具送檢_return");
            }
            if (str.Equals("報廢"))
            {
                update("sp_手工具報廢_return");
            }
        }

        public void update(string procedure)
        {
            try
            {
                
                int check1 = Int32.Parse(txt_領用數量.Text.ToString());
                int check2 = Int32.Parse(txt_歸還數量.Text.ToString());
                if (check1 < check2 || check2 <= 0)
                {
                    MessageBox.Show("0 (<) 歸還數量（<=）" + check1);
                }
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    cmd0 = new SqlCommand(procedure, con);
                    cmd0.CommandType = CommandType.StoredProcedure;
                    cmd0.Parameters.AddWithValue("@項次", txt_項次.Text.ToString());
                    cmd0.Parameters.AddWithValue("@歸還數量", check2);
                    cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                    cmd0.ExecuteNonQuery();
                    MessageBox.Show("歸還成功");
                    con.Close();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_離開_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ManualTool_Weixiu_Return_Load(object sender, EventArgs e)
        {
            if (str.Equals("維修"))
            {
                this.Text = "維修歸還";
                lb_領用.Text = "維修日期";
                lb_數量.Text = "維修數量";
            }
            if (str.Equals("送檢"))
            {
                this.Text = "送檢歸還";
                lb_領用.Text = "送檢日期";
                lb_數量.Text = "送檢數量";
            }
            if (str.Equals("報廢"))
            {
                this.Text = "報廢歸還";
                lb_領用.Text = "報廢日期";
                lb_數量.Text = "報廢數量";
            }
            txt_受理人.Text = 受理人;
            txt_手工具名稱.Text = 手工具名稱;
            txt_手工具編號.Text = 手工具編號;
            txt_歸還日期.Text = 歸還日期;
            txt_項次.Text = 項次;
            txt_領用數量.Text = 領用數量;
            txt_領用日期.Text = 領用日期;
        }

        private void txt_歸還數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("很抱歉，請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
